import requests
import re

class Session:

    def by_session_get_html(self, login_url, url, username, password):

        headers = {
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36"
        }

        data = {
            "ck": "98Yu",
            "name": username,
            "password": password,
            "remember": "false",
            "ticket": ""
        }

        r = requests.session()   #首先创建一个session对象

        r.post(login_url, headers=headers, data=data)      #通过ssession对象来请求登录界面，获取到session

        # 在通过ssession对象来请求我的书架的网站，不需要传cookies，session对象上面获取到了
        html = r.get(url, headers=headers)

        html.encoding = html.apparent_encoding

        return html.text




    def get_html_info(self, login_url, url, username, password, **regex):

        html = self.by_session_get_html(login_url, url, username, password)

        info_dict = {}

        for key, values in regex.items():

            info = re.findall(values, html)

            info_dict[key] = info

        return info_dict